package com.pennywise.controller;

import com.pennywise.common.response.Result;
import com.pennywise.common.util.ExcelUtil;
import com.pennywise.model.CategoryStats;
import com.pennywise.model.Transaction;
import com.pennywise.service.TransactionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.io.File;

@Tag(name = "交易记录管理")
@Slf4j
@RestController
@RequestMapping("/api/transactions")
@RequiredArgsConstructor
public class TransactionController {
    
    private final TransactionService transactionService;
    private final ExcelUtil excelUtil;
    
    @Operation(summary = "添加交易记录")
    @PostMapping
    public Result<Void> addTransaction(@RequestBody Transaction transaction) {
        transactionService.addTransaction(transaction);
        return Result.success(null);
    }
    
    @Operation(summary = "获取交易记录")
    @GetMapping("/{id}")
    public Result<Transaction> getTransaction(@PathVariable Long id) {
        return Result.success(transactionService.getTransaction(id));
    }
    
    @Operation(summary = "更新交易记录")
    @PutMapping("/{id}")
    public Result<Void> updateTransaction(@PathVariable Long id, @RequestBody Transaction transaction) {
        transaction.setId(id);
        transactionService.updateTransaction(transaction);
        return Result.success(null);
    }
    
    @Operation(summary = "删除交易记录")
    @DeleteMapping("/{id}")
    public Result<Void> deleteTransaction(@PathVariable Long id) {
        transactionService.deleteTransaction(id);
        return Result.success(null);
    }
    
    @Operation(summary = "按时间范围查询交易记录")
    @GetMapping("/range")
    public Result<List<Transaction>> getTransactionsByTimeRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        return Result.success(transactionService.getTransactionsByTimeRange(startTime, endTime));
    }
    
    @Operation(summary = "导出交易记录")
    @GetMapping("/export")
    public Result<String> exportTransactions(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        log.info("导出交易记录: {} - {}", startTime, endTime);
        
        List<Transaction> transactions = transactionService.getTransactionsByTimeRange(startTime, endTime);
        if (transactions.isEmpty()) {
            return Result.error("没有可导出的数据");
        }
        
        String fileName = String.format("transactions_%s.xlsx", 
                DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now()));
        String filePath = String.format("%s/data/exports/%s", 
                System.getProperty("user.dir"), fileName);
        
        excelUtil.exportTransactions(transactions, filePath);
        log.info("交易记录导出成功，文件路径: {}", filePath);
        
        return Result.success(filePath);
    }
    
    @Operation(summary = "获取分类统计")
    @GetMapping("/stats/category")
    public Result<List<CategoryStats>> getCategoryStats(
            @RequestParam String type,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        return Result.success(transactionService.getCategoryStats(type, startTime, endTime));
    }
} 